!function(){
    if(/.*habitica.com/.test(window.location.host) && !document.getElementById("habitica-tool")){
        var buildShow = function(o,id){
            return '<div class="hero-stats" data-user-id=' + id + '>'
            + '<div tooltip="健康" class="meter-label">'
            + '<span class="glyphicon glyphicon-heart"></span>'
            + '</div>'
            + '<div class="meter health">'
            + '<div class="bar" style="width: ' + o.hp / o.maxHealth * 100 + '%;"></div>'
            + '<span class="meter-text value ng-binding">' + parseInt(o.hp).toLocaleString() + ' / ' + parseInt(o.maxHealth).toLocaleString() + '</span>'
            + '</div>'
            + '<div tooltip="经验值" class="meter-label">'
            + '<span class="glyphicon glyphicon-star"></span>'
            + '</div>'
            + '<div class="meter experience">'
            + '<div class="bar" style="width: ' + o.exp / o.toNextLevel * 100 + '%;"></div>'
            + '<span class="meter-text value ng-binding">'
            + '<span>'
            + '</span>' + parseInt(o.exp).toLocaleString() + ' / ' + parseInt(o.toNextLevel).toLocaleString() + '</span>'
            + '</div>'
            + '<div tooltip="魔法值" class="meter-label ng-scope">'
            + '<span class="glyphicon glyphicon-fire"></span>'
            + '</div>'
            + '<div class="meter mana ng-scope">'
            + '<div class="bar" style="width: ' + o.mp / o.maxMP * 100 + '%;"></div>'
            + '<span class="meter-text value ng-binding"><span></span>' + parseInt(o.mp).toLocaleString() + ' / ' + parseInt(o.maxMP).toLocaleString() + '</span></div></div>'
        }
        var Teammates = angular.element($('[ng-controller=HeaderCtrl]')[0]).scope().partyMinusSelf
        var flag = 0;
        $('[ng-repeat="profile in partyMinusSelf"]').each(function(){
            $(this).after(buildShow(Teammates[flag].stats,Teammates[flag].id));
            flag++;
        })
        $('body').append('<script id="habitica-tool"></script>');
        var dd = $.parseJSON(window.localStorage['habit-mobile-settings']);
        var done = true;
        var updata = function(){
            $.ajax({
                url:'https://habitica.com/api/v3/groups/' + angular.element($('[ng-controller=HeaderCtrl]')[0]).scope().party.id + '/members?includeAllPublicFields=true',
                headers: {
                    "x-api-key":dd.auth.apiToken,
                    "x-api-user":dd.auth.apiId,
                    "x-client":"habitica-web"
                },
                error: function(XHR,textStatus,errorThrown) {
                    console.log ("XHR="+XHR+"\ntextStatus="+textStatus+"\nerrorThrown=" + errorThrown);
                    done = true;
                },
                cache:false,
                success: function(data,textStatus) {
                    $.each(data.data,function(){
                        var re = $('[data-user-id="' + this.id + '"] .bar')
                        $(re[0]).css('width',this.stats.hp / this.stats.maxHealth * 100 + '%').next()
                        .text(parseInt(this.stats.hp).toLocaleString() + ' / ' + parseInt(this.stats.maxHealth).toLocaleString());
                        $(re[1]).css('width',this.stats.exp / this.stats.toNextLevel * 100 + '%').next()
                        .text(parseInt(this.stats.exp).toLocaleString() + ' / ' + parseInt(this.stats.toNextLevel).toLocaleString());
                        $(re[2]).css('width',this.stats.mp / this.stats.maxMP * 100 + '%').next()
                        .text(parseInt(this.stats.mp).toLocaleString() + ' / ' + parseInt(this.stats.maxMP).toLocaleString());
                    });
                    done = true;
                }
            })
        };
        setInterval(function(){
            if(done){
                done = false;
                updata()
            }
        },1000);
    }
}();